<template>
  <div class="address-page">
    <!-- 头部 -->
    <div class="header">
      <div class="back" @click="$router.back()">
        <i class="iconfont icon-back"></i>
      </div>
      <div class="title">送货地址</div>
    </div>

    <!-- 地址列表 -->
    <div class="address-list" v-if="addressList.length > 0">
      <div class="address-item" v-for="item in addressList" :key="item.daId">
        <div class="info" @click="selectAddress(item)">
          <div class="contact">
            <span class="name">{{ item.contactName }}</span>
            <span class="tel">{{ item.contactTel }}</span>
            <span class="sex">{{ item.contactSex === 1 ? '先生' : '女士' }}</span>
          </div>
          <div class="address">{{ item.address }}</div>
        </div>
        <div class="actions">
          <div class="edit" @click="editAddress(item)">
            <i class="iconfont icon-edit"></i>
            <span>编辑</span>
          </div>
          <div class="delete" @click="removeAddress(item.daId)">
            <i class="iconfont icon-delete"></i>
            <span>删除</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 空状态 -->
    <div class="empty-state" v-else>
      <i class="iconfont icon-address"></i>
      <p>暂无收货地址</p>
    </div>

    <!-- 新增地址按钮 -->
    <div class="add-btn" @click="$router.push('/addDeliveryAddress')">
      <i class="iconfont icon-add"></i>
      <span>新增收货地址</span>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import axios from 'axios'

const router = useRouter()
const route = useRoute()
const addressList = ref([])
const userId = localStorage.getItem('userId')

// 获取地址列表
const getAddressList = async () => {
  try {
    const res = await axios.get('/deliveryAddress/listDeliveryAddressByUserId', {
      params: { userId }
    })
    addressList.value = res.data
  } catch (error) {
    console.error('获取地址列表失败:', error)
  }
}

// 选择地址
const selectAddress = (address) => {
  // 如果是从确认订单页面跳转来的，选择地址后返回
  if (route.query.from === 'order') {
    router.push({
      path: '/order',
      query: {
        businessId: route.query.businessId,
        daId: address.daId
      }
    })
  }
}

// 编辑地址
const editAddress = (address) => {
  router.push({
    path: '/editDeliveryAddress',
    query: { daId: address.daId }
  })
}

// 删除地址
const removeAddress = async (daId) => {
  if (!confirm('确定要删除此地址吗？')) return
  
  try {
    await axios.post('/deliveryAddress/removeDeliveryAddress', null, {
      params: { daId }
    })
    getAddressList() // 重新获取地址列表
  } catch (error) {
    console.error('删除地址失败:', error)
  }
}

onMounted(() => {
  if (!userId) {
    router.push('/login')
    return
  }
  getAddressList()
})
</script>

<style scoped>
.address-page {
  padding-top: 44px;
  padding-bottom: 60px;
  background: #f5f5f5;
  min-height: 100vh;
}

.header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 44px;
  background: #0097ff;
  color: #fff;
  display: flex;
  align-items: center;
  padding: 0 15px;
  z-index: 100;
}

.back {
  padding: 10px;
  margin-left: -10px;
}

.title {
  flex: 1;
  text-align: center;
  font-size: 16px;
}

.address-list {
  margin-top: 10px;
}

.address-item {
  background: #fff;
  padding: 15px;
  margin-bottom: 10px;
  display: flex;
}

.info {
  flex: 1;
  padding-right: 15px;
}

.contact {
  margin-bottom: 5px;
}

.contact .name {
  font-size: 16px;
  margin-right: 10px;
}

.contact .tel {
  color: #666;
  margin-right: 10px;
}

.contact .sex {
  color: #666;
}

.address {
  font-size: 14px;
  color: #666;
}

.actions {
  display: flex;
  align-items: center;
  border-left: 1px solid #eee;
  padding-left: 15px;
}

.edit, .delete {
  padding: 5px 10px;
  color: #666;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.edit {
  color: #0097ff;
}

.delete {
  color: #ff4d4f;
}

.edit i, .delete i {
  font-size: 20px;
  margin-bottom: 3px;
}

.edit span, .delete span {
  font-size: 12px;
}

.empty-state {
  text-align: center;
  padding-top: 100px;
  color: #999;
}

.empty-state i {
  font-size: 60px;
  margin-bottom: 10px;
}

.add-btn {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 50px;
  background: #0097ff;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
}

.add-btn i {
  margin-right: 5px;
}
</style>